﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CNVP.Config;
using CNVP.Framework.Helper;

namespace CNVP.JShop.DAL
{
    public class CNVP_Template
    {
        private string StrSql = "";
        private string Prefix = DBConfig.Prefix;
        #region "读取信息"
        /// <summary>
        /// 读取信息
        /// </summary>
        /// <param name="TemplateID"></param>
        /// <returns></returns>
        public DataTable GetTemplateInfo(int TemplateID)
        {
            StrSql = "Select * From " + Prefix + "Template Where TemplateID=@TemplateID";
            SqlParameter[] param = { DbHelper.MakeInParam("@TemplateID", SqlDbType.Int, 4, TemplateID) };
            return DbHelper.ExecuteTable(StrSql, param);
        }
        /// <summary>
        /// 读取信息
        /// </summary>
        /// <param name="ColumnID"></param>
        /// <param name="TemplateType"></param>
        /// <param name="ProjectName"></param>
        /// <returns></returns>
        public DataTable GetTemplateInfo(int ColumnID, int TemplateType, string ProjectName)
        {
            StrSql = "Select * From " + Prefix + "Template Where ColumnID=@ColumnID And TemplateType=@TemplateType And ProjectName=@ProjectName";
            SqlParameter[] param = { 
                                       DbHelper.MakeInParam("@ColumnID",SqlDbType.Int,4,ColumnID),
                                       DbHelper.MakeInParam("@TemplateType",SqlDbType.Int,4,TemplateType),
                                       DbHelper.MakeInParam("@ProjectName",SqlDbType.VarChar,50,ProjectName)
                                   };
            return DbHelper.ExecuteTable(StrSql, param);
        }
        public DataTable GetTemplateInfo(string ColumnID, int TemplateType, string ProjectName)
        {
            StrSql = "Select top 1 * From " + Prefix + "Template Where ColumnID in ("+ColumnID+") And TemplateType=@TemplateType And ProjectName=@ProjectName order by charindex(CAST(ColumnID as varchar(50)),',"+ColumnID+",') desc";
            SqlParameter[] param = { 
                                       DbHelper.MakeInParam("@TemplateType",SqlDbType.Int,4,TemplateType),
                                       DbHelper.MakeInParam("@ProjectName",SqlDbType.VarChar,50,ProjectName)
                                   };
            return DbHelper.ExecuteTable(StrSql, param);
        }
        #endregion
        #region "添加信息"
        /// <summary>
        /// 添加模板
        /// </summary>
        /// <param name="ColumnID"></param>
        /// <param name="TemplateType"></param>
        /// <param name="ProjectName"></param>
        /// <param name="TemplateUrl"></param>
        public void TemplateAdd(int ColumnID, int TemplateType, string ProjectName, string TemplateUrl)
        {
            StrSql = "Insert Into " + Prefix + "Template (ColumnID,TemplateType,ProjectName,TemplateUrl) Values (@ColumnID,@TemplateType,@ProjectName,@TemplateUrl)";
            SqlParameter[] param = { 
                                       DbHelper.MakeInParam("@ColumnID",SqlDbType.Int,4,ColumnID),
                                       DbHelper.MakeInParam("@TemplateType",SqlDbType.Int,4,TemplateType),
                                       DbHelper.MakeInParam("@ProjectName",SqlDbType.VarChar,50,ProjectName),
                                       DbHelper.MakeInParam("@TemplateUrl",SqlDbType.VarChar,100,TemplateUrl)
                                   };
            DbHelper.ExecuteNonQuery(StrSql, param);
        }
        #endregion
        #region "删除信息"
        /// <summary>
        /// 删除模板
        /// </summary>
        /// <param name="TemplateID"></param>
        public void TemplateDel(int TemplateID)
        {
            StrSql = "Delete From " + Prefix + "Template Where TemplateID=@TemplateID";
            SqlParameter[] param = { 
                                       DbHelper.MakeInParam("@TemplateID",SqlDbType.Int,4,TemplateID)
                                   };
            DbHelper.ExecuteNonQuery(StrSql, param);
        }
        #endregion
    }
}
